"""
Avatar selector module for HeyGen video generator.
Handles avatar selection for video creation.
"""

import sys
import os

# Add the project root directory to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from playwright.async_api import Page


class AvatarSelector:
    """
    Handles avatar selection logic for HeyGen video creation.
    """
    
    async def select_avatar(self, page: Page, avatar_name: str) -> bool:
        """
        Select the specified avatar.
        
        Args:
            page (Page): Playwright page instance
            avatar_name (str): Name of the avatar to select
            
        Returns:
            bool: True if avatar selected successfully, False otherwise
        """
        print(f"Selecting avatar: {avatar_name}")
        
        # Switch to My avatars tab
        await page.click("text=My avatars")
        print("Switched to My avatars tab")
        
        # Wait for tab switch to complete
        await page.wait_for_timeout(3000)
        
        # Try multiple methods to locate and select avatar
        avatar_selected = False
        
        # Method 1: Try using the exact text match for the avatar name
        try:
            await page.locator(f"img[alt='{avatar_name}']").nth(0).click()
            await page.wait_for_timeout(1000)
            await page.locator("div[class='tw-relative']").nth(0).click()
            await page.wait_for_timeout(1000)
            await page.locator("div[class='tw-relative']").nth(0).click()
        except Exception as e:
            print(f"Could not find avatar using text selector: {e}")
        
        await page.wait_for_timeout(3000)
        return True